﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VSDBM.Code.dao;
using VSDBM.Code.dto;

public partial class Visual_CRUD_Group_ListGroup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            VSDBM.Code.dto.User usuariologado = (VSDBM.Code.dto.User)HttpContext.Current.Session["usuariologado"];
            SqlDataSource1.SelectCommand = "SELECT id as ID, description as Description FROM groups WHERE userid=" + usuariologado.id;
            SqlDataSource1.Select(new DataSourceSelectArguments());
            GridGroup.DataSource = SqlDataSource1;
            GridGroup.DataBind();
        }
        
    }
    protected void GridGrupo_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        String aux = e.CommandArgument as String;
        int SelectedRow = Convert.ToInt32(aux);
        if (e.CommandName == "Download")
        {
          int idgroup =Convert.ToInt32(GridGroup.Rows[SelectedRow].Cells[0].Text);
          String description = GridGroup.Rows[SelectedRow].Cells[1].Text;
          DownloadData(idgroup, description);
        }
        else
            if (e.CommandName == "Delete")
            {
                int idgroup = Convert.ToInt32(GridGroup.Rows[SelectedRow].Cells[0].Text);
                VSDBM.Code.dto.User usuariologado = (VSDBM.Code.dto.User)HttpContext.Current.Session["usuariologado"];
                if (!(usuariologado.acessgroup == 1 || usuariologado.acessgroup == 2))
                {
                    HttpContext.Current.Session["exception"] = "You are not authorized!";
                    Response.Redirect("~/Visual/Errors/error.aspx");
                    return;
                }
                GroupsDAO.delete(new Groups(idgroup, "", 0, 0));
                Response.Redirect("../../../Visual/CRUD/Group/ListGroup.aspx");
            }
    }
    

    protected void btsearchonclick(object sender, ImageClickEventArgs e)
    {
        String term = txtterm.Text;
        DataSet ds = GroupsDAO.select( " WHERE UPPER(description) like '%" + term.ToUpper() + "%'" , "");

        if (ds.Tables[0].Rows.Count > 0)
        {
            GridGroup.DataSource = ds.Tables[0];
            GridGroup.DataSourceID = null;
            GridGroup.DataBind();
        }
        else
        {
            Response.Redirect("../../../Visual/Errors/error.aspx?id=42");
        }

    }

    protected void DownloadData(int idgroup, String description)
    {
        String IdList = "";
        List<Sequence> lista = GroupsDAO.selectSequences(idgroup);
        foreach (Sequence seq in lista)
        {
            IdList += seq.idacession;
            IdList += ",";
        }
        
        String FileName = description.Replace(" ", "-");
        Response.ContentType = "text/plain";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName + ".txt");
        Response.Write(IdList);
        Response.End();
    }
}